<template>
  <div id="home">
    <nav-bar class="home-nav">
      <div slot="center">购物街</div>
    </nav-bar>
    <home-swiper :banner="banner" />
    <recommend-view :recommend="recommend" />
    <feature-view />
    <tab-control :titles="['流行','新款','精选']" class="tab-control" @tabClick="tabClick"></tab-control>
    <goods-list :goods="showGoods" />

    <ul>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
      <li>55</li>
    </ul>
  </div>
</template>

<script>
import HomeSwiper from "./childComps/HomeSwiper";
import RecommendView from "./childComps/RecommendView";
import FeatureView from "./childComps/FeatureView";

import NavBar from "components/common/navBar/NavBar";
import TabControl from "components/content/tabControl/TabControl";
import GoodsList from "components/content/goods/GoodsList";

import { getHomeMultidata, getHomeGoods } from "network/home";

export default {
  data() {
    return {
      banner: [],
      dKeyword: [],
      keywords: [],
      recommend: [],
      goods: {
        pop: {
          page: 0,
          list: [],
        },
        new: {
          page: 0,
          list: [],
        },
        sell: {
          page: 0,
          list: [],
        },
      },
      currentType: "pop",
    };
  },
  components: {
    NavBar,
    TabControl,
    GoodsList,
    HomeSwiper,
    RecommendView,
    FeatureView,
  },
  created() {
    // 1 请求多个数据
    this.getHomeMultidata();
    // 2 请求商品数据
    this.getHomeGoods("pop");
    // this.getHomeGoods("new");
    // this.getHomeGoods("sell");
  },
  methods: {
    // 网络请求相关
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        console.log(res);
        this.banner = res.data.banner.list;
        this.dKeyword = res.data.dKeyword.list;
        this.keywords = res.data.keywords.list;
        this.recommend = res.data.recommend.list;
      });
    },
    getHomeGoods(type) {
      const page = this.goods[type].page + 1;
      getHomeGoods(type, page).then((res) => {
        console.log(res);
        this.goods[type].list.push(...res.data.list);
        this.goods[type].page++;
      });
    },

    //事件监听
    tabClick(index) {
      console.log(index);
      switch (index) {
        case 0:
          this.currentType = "pop";
          break;
        case 1:
          this.currentType = "new";
          break;
        case 2:
          this.currentType = "sell";
          break;
      }
    },
  },

  computed: {
    showGoods() {
      return this.goods[currentType].list;
    },
  },
};
</script>

<style scoped>
#home {
  padding-top: 44px;
}
.home-nav {
  background-color: var(--color-tint);
  color: white;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  z-index: 1;
}
.tab-control {
  position: sticky;
  top: 44px;
  z-index: 9;
}
</style>
